{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f2c3496f-5fc3-4975-a812-f5a066dda84f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import fiftyone as fo\n",
    "from pycocotools.coco import COCO\n",
    "import os.path as osp\n",
    "from pycocotools import mask as maskUtils\n",
    "import numpy as np\n",
    "import cv2\n",
    "import glob\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9cb48310-c131-4670-8710-bd909e39de74",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_name=\"nike\"\n",
    "fo.delete_datasets('*')\n",
    "\n",
    "dataset = fo.Dataset(name=dataset_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1769f529-4f05-43b8-9ddc-37edc4367ff0",
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = '/home/user/workspace/datasets/NikeDatasets/ReviewData/0424/'\n",
    "\n",
    "split = \"\"\n",
    "img_dir = osp.join(root_dir, \"images\",split)\n",
    "anno_dir = osp.join(root_dir, \"labels_v2\",split)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "81cc1286-405f-47fa-a984-81c3eecada12",
   "metadata": {},
   "outputs": [],
   "source": [
    "name2id = {\"xiantou\":2, \"zangwu\":1, \"yijiao\":0}\n",
    "id2name = {\"0\":\"yijiao\",\"1\":\"xiantou\",\"2\":\"zangwu\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "19b28a4a-137f-4e15-9d42-a4804bff542f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "samples = []\n",
    "\n",
    "ann_files = os.listdir(anno_dir)\n",
    "task = \"segment\"\n",
    "for ann_file in ann_files:\n",
    "    img_file = osp.join(img_dir, ann_file.replace(\".txt\", \".png\"))\n",
    "    if not osp.exists(img_file):\n",
    "        img_file = img_file.replace(\"0424\", \"0423\")\n",
    "    with open(osp.join(anno_dir,ann_file)) as f:\n",
    "        annotations = f.readlines()\n",
    "    sample = fo.Sample(filepath=img_file)\n",
    "    polygons = []\n",
    "    for ann in annotations:\n",
    "        ann = ann.split(\" \")\n",
    "        class_id = ann[0]\n",
    "        coordinates = list(map(float, ann[1:]))\n",
    "        if task == \"segment\":\n",
    "            polygon_points = [(coordinates[i], coordinates[i+1]) for i in range(0, len(coordinates), 2)]\n",
    "\n",
    "        if task == \"detect\":\n",
    "            x,y,w,h = coordinates\n",
    "            polygon_points = [(x-w/2, y-h/2), (x+w/2, y-h/2), (x+w/2, y+h/2), (x-w/2, y+h/2)]\n",
    "            \n",
    "        polygons.append(\n",
    "            fo.Polyline(\n",
    "                label=f\"{id2name[class_id]}\",\n",
    "                points=[polygon_points],\n",
    "                closed=True,\n",
    "                filled=False,\n",
    "            ))\n",
    "        sample['polylines'] =  fo.Polylines(\n",
    "                                polylines=polygons\n",
    "                            )\n",
    "    samples.append(sample)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f9cc56cd-792a-4e18-906c-3a1a7fbe3064",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 100% |█████████████████| 480/480 [1.0s elapsed, 0s remaining, 471.1 samples/s]         \n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "        <iframe\n",
       "            width=\"100%\"\n",
       "            height=\"800\"\n",
       "            src=\"http://0.0.0.0:8989/?notebook=True&subscription=21e916c5-bb02-4967-b6a5-6d23c801a909\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x7fbc046427b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dataset.add_samples(samples)\n",
    "session = fo.launch_app(dataset, address=\"0.0.0.0\", port=8989)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "c5089275-e844-44f2-b6cb-831ba08403d0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "import os.path as osp\n",
    "\n",
    "err_view = dataset.match_tags(\"err\")\n",
    "\n",
    "# 设置目标文件夹路径\n",
    "save_img = \"/home/tmy/1-Nike数据整理/loss/images\"  # <- 替换成你想要保存的目录\n",
    "#save_label = \"/home/tmy/1-Nike数据整理/loss/labels\"  # <- 替换成你想要保存的目录\n",
    "os.makedirs(save_img, exist_ok=True)\n",
    "#os.makedirs(save_label, exist_ok=True)\n",
    "\n",
    "# 遍历错误图像并移动\n",
    "for it in err_view:\n",
    "    src_path = it['filepath']\n",
    "    img_dst_path = osp.join(save_img, osp.basename(src_path))\n",
    "    shutil.move(src_path, img_dst_path) \n",
    "    #src_path = it['filepath'].replace(\".png\", \".txt\").replace(\"images\", \"labels\")\n",
    "    #lab_dst_path = osp.join(save_label, osp.basename(src_path))\n",
    "    #shutil.move(src_path, lab_dst_path) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aab352da-6176-4d09-b41b-edb12c3ef754",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
